<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .gray{
            background-color: gray;
        }
        .red{
            background-color: red;
        }
        .yellow{
            background-color: yellow;
        }
        .green{
            background-color: green;
        }
        .box{
            border: 1px solid black;
            display: inline-flex;
        }
        #red,#green,#yellow,#count{
            width: 100px;
            height: 100px;
            border-radius: 50px;
            margin: 10px;
            line-height: 100px;
            font-size: 50px;
            color: #FFF;
            text-align: center;
        } 
        .count{
            line-height: 100px;
            font-size: 60px;
        }
    </style>
</head>
<body>
    <div class="box">
        <div id="red" class="red">30</div>
        <div id="yellow" class="yellow">5</div>
        <div id="green" class="green">35</div>
    </div>
    <script>
        var lamp={
            red:{
                obj:document.getElementById("red"),
                timeout:30,
                style:['red','gray','gray'],
                next:'green'

            },
            yellow:{
                obj:document.getElementById("yellow"),
                timeout:5,
                style:['gray','yellow','gray'],
                next:'red'

            },
            green:{
                obj:document.getElementById("green"),
                timeout:35,
                style:['gray','gray','green'],
                next:'yellow'

            },
            changeStyle:function(style) {
                this.red.obj.className=style[0];
                this.yellow.obj.className=style[1];
                this.green.obj.className=style[2];
            },
            changeTime(num) {
                //1.清楚所有灯的时间显示
                this.red.obj.innerHTML='';
                this.yellow.obj.innerHTML='';
                this.green.obj.innerHTML='';
                //2.设置当前灯的时间
                now.obj.innerHTML=num<10?('0'+num):num;
            }
        }
        
        var now=lamp.green;
        var timeout=now.timeout;
        lamp.changeStyle(now.style);
        lamp.changeTime(now);
        setInterval(function(){
            if(--timeout<=0){
                //换灯
                now=lamp[now.next];
                //设置时间
                timeout=now.timeout;
                //改变灯的样式
                lamp.changeStyle(now.style);
            }
            //减少时间显示
            lamp.changeTime(timeout);
        },1000);
    </script>
</body>
</html>